package com.sakura.mall.member.growthChangeHistory.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.member.growthChangeHistory.domain.entity.GrowthChangeHistoryEntity;
import com.sakura.mall.member.growthChangeHistory.domain.request.AddGrowthChangeHistoryRequest;
import com.sakura.mall.member.growthChangeHistory.domain.request.UpdateGrowthChangeHistoryRequest;
import com.sakura.mall.member.growthChangeHistory.domain.request.GrowthChangeHistoryPageRequest;
import com.sakura.mall.member.growthChangeHistory.domain.vo.GrowthChangeHistoryVo;
import com.sakura.mall.member.growthChangeHistory.mapper.GrowthChangeHistoryMapper;
import com.sakura.mall.member.growthChangeHistory.service.GrowthChangeHistoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 成长值变化历史记录 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class GrowthChangeHistoryServiceImpl extends ServiceImpl<GrowthChangeHistoryMapper, GrowthChangeHistoryEntity> implements GrowthChangeHistoryService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddGrowthChangeHistoryRequest request) {
        // todo 其他校验逻辑
        GrowthChangeHistoryEntity growthChangeHistory = request.convert(GrowthChangeHistoryEntity.class);
        this.save(growthChangeHistory);
    }

    @Override
    public Page<GrowthChangeHistoryVo> queryPage(GrowthChangeHistoryPageRequest request) {
        IPage<GrowthChangeHistoryEntity> qp = request.toPage();
        LambdaQueryWrapper<GrowthChangeHistoryEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<GrowthChangeHistoryVo> ret = qp.convert(entity -> entity.convert(GrowthChangeHistoryVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateGrowthChangeHistoryRequest request) {
        GrowthChangeHistoryEntity entity = request.convert(GrowthChangeHistoryEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

